VFG.TiandituImageryProvider = function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.id = options.id;
	this.name = options.name;
	this.code = options.code;
	this.type = options.type;
	this.imagery = options.thing;
	this.subdomains=['0','1','2','3','4','5','6','7'];
	this.tdtUrl = 'https://t{s}.tianditu.gov.cn/';
	this.init();
}

//初始化
VFG.TiandituImageryProvider.prototype.init = function() {
	let provider;
	if('terrain'==this.imagery.classify){
		provider=this.getTerrain();
		if(provider) this.ImageryProvider=this.viewer.terrainProvider = provider;
	}else{
		if('img_w'==this.imagery.type){
			provider=this.getImg_w();
			if(provider) this.ImageryProvider=this.viewer.imageryLayers.addImageryProvider(provider);
		}
		else if('cia_w'==this.imagery.type){
			provider=this.getCia_w();
			if(provider) this.ImageryProvider=this.viewer.imageryLayers.addImageryProvider(provider);
		}
		else if('ibo_w'==this.imagery.type){
			provider=this.getIbo_w();
			if(provider) this.ImageryProvider=this.viewer.imageryLayers.addImageryProvider(provider);
		}
		else if('ter_w'==this.imagery.type){
			provider=this.getTer_w();
			if(provider) this.ImageryProvider=this.viewer.imageryLayers.addImageryProvider(provider);
		}
		else if('vec_w'==this.imagery.type){
			provider=this.getVec_w();
			if(provider) this.ImageryProvider=this.viewer.imageryLayers.addImageryProvider(provider);
		}
	}
};


//影像底图
VFG.TiandituImageryProvider.prototype.getImg_w = function() {
/*	return new Cesium.WebMapTileServiceImageryProvider({
		url: "http://t{s}.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk="+this.imagery.auth,
	      subdomains: this.subdomains,
	      layer: "tdtImgLayer",
	      style: "default",
	      format: "image/jpeg",
	      tileMatrixSetID: "GoogleMapsCompatible",
	      show: true
	});*/
	return new Cesium.UrlTemplateImageryProvider({
	    url: this.tdtUrl + 'DataServer?T=img_w&x={x}&y={y}&l={z}&tk=' + this.imagery.auth,
	    subdomains: this.subdomains,
	    tilingScheme : new Cesium.WebMercatorTilingScheme(),
	    maximumLevel : 18
	});
};


//影像注记
VFG.TiandituImageryProvider.prototype.getCia_w = function() {
	return new Cesium.UrlTemplateImageryProvider({
	    url: this.tdtUrl + 'DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=' + this.imagery.auth,
	    subdomains: this.subdomains,
	    tilingScheme : new Cesium.WebMercatorTilingScheme(),
	    maximumLevel : 18
	});
	
/*	return new Cesium.WebMapTileServiceImageryProvider({
		url: "http://t{s}.tianditu.com/cia_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=cia&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default.jpg&tk="+this.imagery.auth,
	    subdomains: this.subdomains,
	    layer: "tdtCiaLayer",
	    style: "default",
	    format: "image/jpeg",
	    tileMatrixSetID: "GoogleMapsCompatible",
	    show: true
	});*/
};

//地形晕渲
VFG.TiandituImageryProvider.prototype.getTer_w = function() {
	return new Cesium.UrlTemplateImageryProvider({
	    url: this.tdtUrl + 'DataServer?T=ter_w&x={x}&y={y}&l={z}&tk=' + this.imagery.auth,
	    subdomains: this.subdomains,
	    tilingScheme : new Cesium.WebMercatorTilingScheme(),
	    maximumLevel : 18
	});
};

//矢量
VFG.TiandituImageryProvider.prototype.getVec_w = function() {
	return new Cesium.UrlTemplateImageryProvider({
	    url: this.tdtUrl + 'DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=' + this.imagery.auth,
	    subdomains: this.subdomains,
	    tilingScheme : new Cesium.WebMercatorTilingScheme(),
	    maximumLevel : 18
	});
};

//叠加地形服务
VFG.TiandituImageryProvider.prototype.getTerrain = function() {
	var terrainUrls = new Array();
	for (var i = 0; i < this.subdomains.length; i++){
	    var url = this.tdtUrl.replace('{s}', this.subdomains[i]) + 'mapservice/swdx?tk=' + this.imagery.auth;
	    terrainUrls.push(url);
	}
	return new Cesium.GeoTerrainProvider({
	    urls: terrainUrls
	});
};

//叠加国界服务
VFG.TiandituImageryProvider.prototype.getIbo_w = function() {
	return new Cesium.UrlTemplateImageryProvider({
	    url: this.tdtUrl + 'DataServer?T=ibo_w&x={x}&y={y}&l={z}&tk=' + this.imagery.auth,
	    subdomains: this.subdomains,
	    tilingScheme : new Cesium.WebMercatorTilingScheme(),
	    maximumLevel : 10
	})
};

VFG.TiandituImageryProvider.prototype.destroy = function() {
	if(this.ImageryProvider){
		if('terrain'!=this.imagery.classify){
			this.viewer.imageryLayers.remove(this.ImageryProvider);
		}else{
			this.viewer.terrainProvider= new Cesium.EllipsoidTerrainProvider({});
		}
	}
	delete this.ImageryProvider
	delete this.options
    delete this.viewer
	return Cesium.destroyObject(this);
};
